package net.dopan.pigframe.user.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xiaoleilu.hutool.collection.CollUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.dopan.pigframe.common.util.TreeUtil;
import net.dopan.pigframe.core.base.controller.BaseController;
import net.dopan.pigframe.core.base.controller.response.ResponseBean;
import net.dopan.pigframe.core.model.MenuVO;
import net.dopan.pigframe.core.utils.CommonConstant;
import net.dopan.pigframe.user.model.dto.MenuTree;
import net.dopan.pigframe.user.model.entity.SysMenu;
import net.dopan.pigframe.user.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Package: net.dopan.pigframe.user.controller
 * @Title: MenuController
 * @Description: 菜单相关
 * @author: 刘宽
 * @date: 2018/9/2 21:25
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单接口(API)", description = "菜单相关操作")
public class MenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 通过角色名称查询用户菜单
     *
     * @param role 角色名称
     * @return 菜单列表
     */
    @GetMapping("/findMenuByRole/{role}")
    @ApiOperation(value = "通过角色名称查询用户菜单", notes = "通过角色名称查询用户菜单")
    public List<MenuVO> findMenuByRole(@PathVariable String role) {
        return sysMenuService.findMenuByRoleName(role);
    }

    /**
     * 返回当前用户的树形菜单集合
     *
     * @return 当前用户的树形菜单
     */
    @GetMapping(value = "/userMenu")
    @ApiOperation(value = "返回当前用户的树形菜单集合", notes = "返回当前用户的树形菜单集合")
    public List<MenuTree> userMenu() {
        // 获取符合条件得菜单
        Set<MenuVO> all = new HashSet<>();
        getRole().forEach(roleName -> all.addAll(sysMenuService.findMenuByRoleName(roleName)));
        List<MenuTree> menuTreeList = new ArrayList<>();
        all.forEach(menuVo -> {
            if (CommonConstant.MENU.equals(menuVo.getType())) {
                menuTreeList.add(new MenuTree(menuVo));
            }
        });
        CollUtil.sort(menuTreeList, Comparator.comparingInt(MenuTree::getSort));
        return TreeUtil.bulid(menuTreeList, -1);
    }

    /**
     * 返回树形菜单集合
     *
     * @return 树形菜单
     */
    @GetMapping(value = "/allTree")
    @ApiOperation(value = "返回树形菜单集合", notes = "返回树形菜单集合")
    public List<MenuTree> getTree() {
        SysMenu condition = new SysMenu();
        condition.setDelFlag(CommonConstant.STATUS_NORMAL);
        return TreeUtil.bulidTree(sysMenuService.selectList(new EntityWrapper<>(condition)), -1);
    }
    
    /**
     * 返回角色的菜单集合
     *
     * @param roleName 角色名称
     * @return 属性集合
     */
    @GetMapping("/roleTree/{roleName}")
    @ApiOperation(value = "返回角色的菜单集合", notes = "返回角色的菜单集合")
    public List<Integer> roleTree(@PathVariable String roleName) {
        List<MenuVO> menus = sysMenuService.findMenuByRoleName(roleName);
        List<Integer> menuList = new ArrayList<>();
        for (MenuVO menuVo : menus) {
            menuList.add(menuVo.getMenuId());
        }
        return menuList;
    }

    /**
     * 通过ID查询菜单的详细信息
     *
     * @param id 菜单ID
     * @return 菜单详细信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询菜单的详细信息", notes = "通过ID查询菜单的详细信息")
    public SysMenu menu(@PathVariable Integer id) {
        return sysMenuService.selectById(id);
    }

    /**
     * 新增菜单
     *
     * @param sysMenu 菜单信息
     * @return success/false
     */
    @PostMapping
    @ApiOperation(value = "新增菜单", notes = "新增菜单")
    public ResponseBean<Boolean> menu(@RequestBody SysMenu sysMenu) {
        return new ResponseBean<>(sysMenuService.insert(sysMenu));
    }

    /**
     * 删除菜单
     *
     * @param id 菜单ID
     * @return success/false
     * TODO  级联删除下级节点
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除菜单", notes = "删除菜单")
    public ResponseBean<Boolean> menuDel(@PathVariable Integer id) {
        return new ResponseBean<>(sysMenuService.deleteMenu(id));
    }

    /**
     * 更新菜单
     *
     * @param sysMenu 菜单信息
     * @return success/false
     */
    @PutMapping
    @ApiOperation(value = "更新菜单", notes = "更新菜单")
    public ResponseBean<Boolean> menuUpdate(@RequestBody SysMenu sysMenu) {
        return new ResponseBean<>(sysMenuService.updateMenuById(sysMenu));
    }

}
